package com.xds.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xds.gulimall.product.entity.AttrEntity;
import com.xds.gulimall.product.service.AttrAttrgroupRelationService;
import com.xds.gulimall.product.service.AttrService;
import com.xds.gulimall.product.service.CategoryService;
import com.xds.gulimall.product.vo.AttrGroupRelationVo;
import com.xds.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xds.gulimall.product.entity.AttrGroupEntity;
import com.xds.gulimall.product.service.AttrGroupService;
import com.xds.common.utils.PageUtils;
import com.xds.common.utils.R;



/**
 * 属性分组
 *
 * @author baixu
 * @email baixu@gmail.com
 * @date 2021-11-09 17:16:06
 */
@RestController
@RequestMapping("product/attrgroup")

public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    AttrService attrService;
    @Autowired
    AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{cateLogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("cateLogId") Long cateLogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page  =  attrGroupService.queryPage(params,cateLogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
   // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path=   categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    //属性分股关联
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId")Long attrgroupId){
     List<AttrEntity> attrEntityList=   attrService.getRelationAttr(attrgroupId);
     return R.ok().put("data",attrEntityList);
    }
    @DeleteMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId")Long attrgroupId,@RequestParam Map<String,Object> params){
     PageUtils page=   attrService.getNoRelationAttr(params,attrgroupId);
     return R.ok().put("page",page);

    }
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List <AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
     List<AttrGroupWithAttrsVo>  vos=  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
     return R.ok().put("data",vos);


    }


}
